using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Devices.Sensors;

namespace SimplePong
{
    public class Player
    {
        Texture2D texture;
        Vector2 position;
        Rectangle rectangle;
        Accelerometer accelSensor;
        Vector3 accelReading = new Vector3();

        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        public Rectangle Rectangle
        {
            get { return rectangle; }
            set { rectangle = value; }
        }


        public Player(int player)
        {
            texture = GameGlobals.content.Load<Texture2D>("Textures/player");

            if (player == 1)
            {
                //Left Player
                position = new Vector2(25, GameGlobals.graphicsManager.PreferredBackBufferHeight/2 - texture.Height/2);
            }
            else
            {
                //Ricght Player
                position = new Vector2(GameGlobals.graphicsManager.PreferredBackBufferWidth - texture.Width - 25, 
                    GameGlobals.graphicsManager.PreferredBackBufferHeight / 2 - texture.Height / 2);
            }

            rectangle = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
            accelSensor = new Accelerometer();

            // Add the accelerometer event handler to the accelerometer sensor.
            accelSensor.ReadingChanged +=
                new EventHandler<AccelerometerReadingEventArgs>(AccelerometerReadingChanged);

            // Start the accelerometer
            try
            {
                accelSensor.Start();
            }
            catch (AccelerometerFailedException e)
            {
                // the accelerometer couldn't be started.  No fun!
                //accelActive = false;
            }
        }


        public void Update()
        {


            // accelerate depending on accelerometer
            position.Y += accelReading.Z * 50;


            if (position.Y <= 0)
            {
                position.Y = 0;
            }
            if (position.Y + texture.Height >= GameGlobals.graphicsManager.PreferredBackBufferHeight)
            {
                position.Y = GameGlobals.graphicsManager.PreferredBackBufferHeight - texture.Height;
            }

            UpdateRectangle();
        }

        public void Draw(SpriteBatch spritebatch)
        {
            spritebatch.Draw(texture, position, Color.White);
        }


        public void AccelerometerReadingChanged(object sender, AccelerometerReadingEventArgs e)
        {
            accelReading.X = (float)e.X;
            accelReading.Y = (float)e.Y;
            accelReading.Z = (float)e.Z;
        }

        private void UpdateRectangle()
        {
            rectangle.X = (int)position.X;
            rectangle.Y = (int)position.Y;
        }
    }
}
